package com.hiultra.archive.controller;

import java.net.URISyntaxException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hiultra.archive.domain.ArchiveBorrow;
import com.hiultra.archive.domain.ArchiveStocktakingTask;
import com.hiultra.archive.service.IArchiveBorrowService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.archive.domain.ArchiveBorrowTask;
import com.hiultra.archive.service.IArchiveBorrowTaskService;
import com.hiultra.common.utils.poi.ExcelUtil;
import com.hiultra.common.core.page.TableDataInfo;

/**
 * 借阅任务Controller
 *
 * @author ls
 * @date 2024-07-19
 */
@RestController
@RequestMapping("/archive/borrowTask")
public class ArchiveBorrowTaskController extends BaseController {
    @Autowired
    private IArchiveBorrowTaskService archiveBorrowTaskService;
    @Autowired
    private IArchiveBorrowService archiveBorrowService;

    /**
     * 查询借阅任务列表
     */
    @PreAuthorize("@ss.hasPermi('archive:borrowTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArchiveBorrowTask archiveBorrowTask) {
        startPage();
        List<ArchiveBorrowTask> list = archiveBorrowTaskService.selectArchiveBorrowTaskList(archiveBorrowTask);
        return getDataTable(list);
    }

    /**
     * 导出借阅任务列表
     */
    @PreAuthorize("@ss.hasPermi('archive:borrowTask:export')")
    @Log(title = "借阅任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArchiveBorrowTask archiveBorrowTask) {
        List<ArchiveBorrowTask> list = null;
        try {
            list = archiveBorrowTaskService.selectArchiveBorrowTaskList(archiveBorrowTask);

            for (ArchiveBorrowTask archiveBorrowTask1 : list) {
                List<ArchiveBorrow> archiveBorrowList = archiveBorrowService.selectArchiveBorrowList(new ArchiveBorrow() {{
                    setTaskId(archiveBorrowTask1.getTaskId());
                }});

                archiveBorrowTask1.setBorrowInfo(archiveBorrowList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ExcelUtil<ArchiveBorrowTask> util = new ExcelUtil<ArchiveBorrowTask>(ArchiveBorrowTask.class);
        util.exportExcel(response, list, "借阅任务数据");
    }

    /**
     * 获取借阅任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('archive:borrowTask:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") String taskId) {
        return AjaxResult.success(archiveBorrowTaskService.selectArchiveBorrowTaskByTaskId(taskId));
    }

    /**
     * 根据借阅任务主键获取档案盒信息
     */
    @PreAuthorize("@ss.hasPermi('archive:borrowTask:getBox')")
    @PostMapping(value = "/selectArcBoxById")
    public AjaxResult getBox(@RequestBody String taskId) {
        return AjaxResult.success(archiveBorrowTaskService.selectArcBoxByTaskId(taskId));
    }

    /**
     * 新增借阅任务
     */
    @PreAuthorize("@ss.hasPermi('archive:borrowTask:add')")
    @Log(title = "借阅任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArchiveBorrowTask archiveBorrowTask) {
        return toAjax(archiveBorrowTaskService.insertArchiveBorrowTask(archiveBorrowTask));
    }

    /**
     * 修改借阅任务
     */
    @PreAuthorize("@ss.hasPermi('archive:borrowTask:edit')")
    @Log(title = "借阅任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArchiveBorrowTask archiveBorrowTask) {
        return toAjax(archiveBorrowTaskService.updateArchiveBorrowTask(archiveBorrowTask));
    }

    /**
     * 修改借阅任务
     */
    @PreAuthorize("@ss.hasPermi('archive:borrowTask:editBorrow')")
    @PostMapping("/editBorrow")
    public AjaxResult editBorrow(@RequestBody List<String> Ids) {
        return toAjax(archiveBorrowTaskService.editBorrowById(Ids, getUsername()));
    }

    /**
     * 删除借阅任务
     */
    @PreAuthorize("@ss.hasPermi('archive:borrowTask:remove')")
    @Log(title = "借阅任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable String[] taskIds) {
        return toAjax(archiveBorrowTaskService.deleteArchiveBorrowTaskByTaskIds(taskIds));
    }

    /**
     * 档案借阅任务结果上报接口
     */
    @ApiOperation("档案借阅任务结果上报接口")
    @PostMapping("/archiveBorrowTaskResultReport")
    @PreAuthorize("@ss.hasPermi('archive:borrowTask:archiveBorrowTaskResultReport')")
    public AjaxResult archiveBorrowTaskResultReport(@RequestBody ArchiveBorrowTask archiveBorrowTask) throws JsonProcessingException, URISyntaxException {
        if (null == archiveBorrowTask) {
            return AjaxResult.error("借阅任务信息为空，请检查重试！");
        }
        if(null==archiveBorrowTask.getBorrowNo()){
            return AjaxResult.error("任务单号信息为空，请检查重试！");
        }
        return AjaxResult.success(archiveBorrowTaskService.archiveBorrowTaskResultReport(archiveBorrowTask));
    }
}
